<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="top">
            <el-main class="nopadding">
                <!-- 毛料明细 -->
                <el-col :span="24">
                    <el-form-item label="毛料明细" prop="material_data">
                        <!-- <el-button type="primary" size="small" icon="el-icon-plus" @click="openWu">新增毛料</el-button> -->
                        <sc-form-table ref="table" v-model="form.material_data" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                            <el-table-column prop="material_name" label="毛料名称" align="center"></el-table-column>
                            <el-table-column prop="skc" label="毛料skc" align="center"></el-table-column>
                            <el-table-column prop="apply_name" label="供应商" align="center"></el-table-column>
                            <el-table-column prop="color_number" label="色号" align="center" width="100"></el-table-column>
                            <el-table-column prop="colour" label="颜色" align="center" width="100"></el-table-column>
                            <el-table-column prop="reality_weight" label="数量" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.reality_weight" placeholder="请输入数量"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column prop="batch_number" label="缸号" align="center">
                                <template #default="scope">
                                    <el-select v-model="scope.row.batch_number" clearable style="width: 100%" placeholder="请选择" @change="batchChange(scope.row)">
                                        <el-option v-for="item in scope.row.batch_stock" :key="item.batch_no" :label="item.batch_no+'【'+item.curr_stock+'KG】'" :value="item.batch_no" />
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column prop="store_id" label="仓库" align="center">
                                <template #default="scope">
                                    <div>
                                        {{scope.row.ware_name}}
                                    </div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="remark" label="备注" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                                </template>
                            </el-table-column>
                        </sc-form-table>
                    </el-form-item>
                </el-col>
                <!-- 辅料明细 -->
                <!-- <el-col :span="24">
                    <el-form-item label="辅料明细" prop="accessories_data">
                        <sc-form-table ref="table" v-model="form.accessories_data" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                            <el-table-column prop="material_name" label="辅料名称" align="center"></el-table-column>
                            <el-table-column prop="skc" label="辅料skc" align="center"></el-table-column>
                            <el-table-column prop="apply_name" label="供应商" align="center"></el-table-column>
                            <el-table-column prop="color_number" label="色号" align="center"></el-table-column>
                            <el-table-column prop="colour" label="颜色" align="center"></el-table-column>
                            <el-table-column prop="reality_weight" label="数量" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.reality_weight" placeholder="请输入数量"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column prop="batch_number" label="缸号" align="center">
                                <template #default="scope">
                                    <el-select v-model="scope.row.batch_number" clearable style="width: 100%" placeholder="请选择" @change="batchChange(scope.row)">
                                        <el-option v-for="item in scope.row.batch_stock" :key="item.batch_no" :label="item.batch_no+'【'+item.curr_stock+'KG】'" :value="item.batch_no" />
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column prop="store_id" label="仓库" align="center">
                                <template #default="scope">
                                    <div>
                                        {{scope.row.ware_name}}
                                    </div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="remark" label="备注" align="center">
                                <template #default="scope">
                                    <el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                                </template>
                            </el-table-column>
                        </sc-form-table>
                    </el-form-item>
                </el-col> -->
            </el-main>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="submit">确定</el-button>
		</template>
	</el-dialog>
    <check-marter v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable :query="{category_id:type== 1 ? 1 : 2}"></check-marter>
</template>

<script>
import checkMarter from '../../produceOrder/wu.vue';
export default {
  components: { checkMarter },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择工序',
				edit: '补毛采购单',
				show: '查看'
			},
            dialog: {
				save: false,
			},
			visible: false,
			isSaveing: false,
            dialogVisible:false,
            form: {
                contract_no:"",
                material_data:[], //辅料信息
                accessories_data:[],
            },
            message:"",
            type:""
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        async setData(data) {
            this.form.contract_no = data.contract_no
            var res = await this.$API.produce.proDetail.get({contract_no:data.contract_no});
            // 毛料明细
            let material  =[]
            if(res.data.material_data!=''){
                res.data.material_data.map(item=>{
                    let detail = {
                        material_name:item.material_name,//毛料名称
                        material_spec_code:item.material_spec_code,//毛料编码
                        skc:item.materialSpec?item.materialSpec.skc:"",//毛料编码
                        colour:item.materialSpec?item.materialSpec.colour:"",//毛料颜色
                        color_number:item.materialSpec?item.materialSpec.color_number:"",//毛料色号
                        apply_name:item.materialSpec?item.materialSpec.apply_name:"",//
                        material_id:item.material_id,//毛料ID
                        total_weight:item.residue_weight,//所需重量
                        reality_weight:item.residue_weight,//实发重量
                        batch_number:"",//缸号
                        batch_stock:item.batch_stock,
                    }
                    material.push(detail)
                })
                this.form.material_data = material
                // 物料明细
                // let accessories  =[]
                // res.data.accessories_data.map(item=>{
                //     let detail = {
                //         material_name:item.material_name,//辅料名称
                //         material_spec_code:item.material_spec_code,//辅料编码
                //         colour:item.materialSpec?item.materialSpec.colour:"",//辅料颜色
                //         color_number:item.materialSpec?item.materialSpec.color_number:"",//辅料色号
                //         apply_name:item.materialSpec?item.materialSpec.apply_name:"",//
                //         // ratio:item.ratio,//用量
                //         // loss_ratio:item.loss_ratio,//损耗
                //         material_id:item.material_id,//辅料ID
                //         total_weight:item.residue_weight,//所需重量
                //         reality_weight:item.residue_weight,//实发重量
                //         batch_number:"",//缸号
                //         batch_stock:item.batch_stock
                //     }
                //     accessories.push(detail)
                // })
                // this.form.accessories_data = accessories
            }
		},
        // 打开弹窗
        // openWu(){
        //     this.type = 1
        //     this.dialog.save = true
        //     this.$nextTick(() => {
        //         this.$refs.saveDialog.open('add')
        //     })
        // },
        // 辅料
        // openWuFu(){
        //     this.type = 2
        //     this.dialog.save = true
        //     this.$nextTick(() => {
        //         this.$refs.saveDialog.open('add')
        //     })
        // },
        // handleSuccess(data, mode) {
        //     console.log('data :>> ', data);
        //     let a = []
        //     let b = []
        //     data.map((item)=>{
        //         a.push({
        //             material_id:item.material.material_id,
        //             material_spec_code:item.spec_code,
        //             skc:item.skc,
        //             category_name:item.material.category_name,
        //             material_name:item.material.material_name,
        //             color_number:item.color_number,
        //             colour:item.colour,
        //             image:item.image,
        //             apply_name:item.material.apply_name,
        //             remark:item.remark,
        //             unit:item.material.unit_name
        //         })
        //     })
        //     let existingIds = this.form.material_data.map(item => item.material_spec_code);
        //     for(let i = a.length - 1; i >= 0; i--){
        //         if(existingIds.includes(a[i].material_spec_code)){
        //             a.splice(i, 1);
        //         }
        //     }
        //     let existingIds1 = this.form.accessories_data.map(item => item.material_spec_code);
        //     for(let i = a.length - 1; i >= 0; i--){
        //         if(existingIds1.includes(a[i].material_spec_code)){
        //             a.splice(i, 1);
        //         }
        //     }
        //     if(mode==1){
        //         this.form.material_data = [...this.form.material_data,...a]
        //     }else{
        //         this.form.accessories_data = [...this.form.accessories_data,...a]
        //     }
        //     this.form.material_data.map(item=>{
        //         if(b.indexOf(item.material_name) == -1) {
        //             b.push(item.material_name)
        //         }

        //     })
        //     this.form.wool_seed = b.join("+")
        // },
        count(row){
            row.weight = ((row.ratio * this.allWeight/100) * (1+row.loss_ratio/100)).toFixed(2)
        },
        counts(row){
            row.weight = (row.ratio * this.allNum * (1+row.loss_ratio/100)).toFixed(2)
        },
        batchChange(row){
            row.batch_stock.map(item=>{
                if(item.batch_no==row.batch_number){
                    row.store_area_id = item.area_id,
                    row.store_location_id = item.location_id,
                    row.store_id = item.warehouse_id,
                    row.ware_name = item.ware_name
                    // row.reality_weight = item.curr_stock
                }
            })
        },
        //表单提交方法
        submit(){
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    var res  = [];
                    res = await this.$API.produce.contract.repairMater.post(this.form);
                    if(res.code == 1){
                        this.$emit('success', this.form, this.mode)
                        this.visible = false;
                        this.$message.success("操作成功")
                    }else if(res.code==2){
                        this.dialogVisible = true
                        this.message = res.message
                    }else{
                        this.$alert(res.message, "提示", {type: 'error'})
                        this.isSaveing = false;

                    }
                }else{
                    return false;
                }
            })
        },
	}
}
</script>

<style></style>
